package com.satan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.satan.entity.SysMenu;
import com.satan.entity.response.ResCode;
import com.satan.mapper.SysMenuMapper;
import com.satan.service.SysMenuService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @author Demon
* @description 针对表【sys_menu】的数据库操作Service实现
* @createDate 2023-10-03 20:53:34
*/
@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu>
    implements SysMenuService{
    @Override
    public List<SysMenu> buildTreeMenu(List<SysMenu> sysMenuList) {
        List <SysMenu> resultMenuList=new ArrayList<SysMenu>();
        for (SysMenu sysMenu : sysMenuList) {
            for (SysMenu menu : sysMenuList) {
                if (menu.getParentId()==sysMenu.getId()){
                    sysMenu.getChildren().add(menu);
                }
            }
            if (sysMenu.getParentId()==0L){
                resultMenuList.add(sysMenu);
            }
        }
        return resultMenuList;
    }

    @Override
    public boolean saveOrUpdateMenu(SysMenu sysMenu) {
        boolean b;
        if(sysMenu.getId()==null || sysMenu.getId()==-1){
            sysMenu.setId(null);
            b=save(sysMenu);
        }else{
            b=updateById(sysMenu);
        }
        return b;
    }

    @Override
    public ResCode deleteMenu(Long id) {
        long count = count(new QueryWrapper<SysMenu>().eq("parent_id", id));
        if(count>0){
            return ResCode.CHILD_EXITS;
        }
        removeById(id);
        return null;
    }
}




